package com.netintech.project.system.scheduler;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.netintech.common.utils.hikSdk.HikSdkUtil;
import com.netintech.common.utils.hikSdk.LoginResult;
import com.netintech.project.system.domain.NvrCamNotRecordPeriod;
import com.netintech.project.system.domain.bo.NvrInfo;
import com.netintech.project.system.domain.bo.NvrInfos;
import com.netintech.project.system.service.HikSdkService;
import com.sun.jna.NativeLong;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.concurrent.ExecutorService;

/**
 * 生成报警数据，并向前端推送
 *
 * @author luda
 */
@Slf4j
@Component
@EnableConfigurationProperties(NvrInfos.class)
public class QueryNotRecordPeriodBySdk {
    @Autowired
    private HikSdkService hikSdkService;
    private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
    @Autowired
    private ExecutorService executor;
    @Autowired
    private NvrInfos nvrInfos;
    private DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

    @Scheduled(cron = "0 0 1 * * *")
    public void record() {
        LocalDate yesterday = LocalDate.now().plusDays(-1L);
        try {
            List<NvrCamNotRecordPeriod> nvrCamNotRecordPeriods = hikSdkService.queryRecordContinuityBatch(nvrInfos,
                    LocalDateTime.of(yesterday.getYear(), yesterday.getMonthValue(), yesterday.getDayOfMonth(), 0, 0, 0),
                    LocalDateTime.of(yesterday.getYear(), yesterday.getMonthValue(), yesterday.getDayOfMonth(), 23, 59, 59));
            if (!CollectionUtils.isEmpty(nvrCamNotRecordPeriods)) {
                hikSdkService.insertBatch(nvrCamNotRecordPeriods);
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("定时记录相机未录像时间段失败！", e);
        }
    }
}
